package cn.com.majiang.service.impl;

import cn.com.majiang.dao.ManagerDao;
import cn.com.majiang.domain.Manager;
import cn.com.majiang.param.ManagerParam;
import cn.com.majiang.service.IManagerService;
import cn.com.majiang.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangyanxin on 2017/5/28.
 */
@Service
public class ManagerServiceImpl implements IManagerService {
    Logger logger = LoggerFactory.getLogger(ManagerServiceImpl.class);
    @Autowired
    ManagerDao managerDao;
    @Override
    public Map<String, Object> searchManagerList(ManagerParam param) {
        int count = managerDao.selectManagerCount(param);
        if(param.getPage() != null) {
            param.setStartNum(param.getLimit() * (param.getPage() - 1));
        }
        List<Manager> list = managerDao.selectManagerList(param);
        if(list == null || list.size() == 0){
            list = new ArrayList<>();
        }
        Map map = new HashMap();
        map.put("rows",list);
        map.put("count",count);
        return map;
    }

    @Override
    public Boolean editManagerStatus(Manager manager) {
        return managerDao.updateManagerStatus(manager) > 0;
    }

    @Override
    public Boolean saveManager(Manager manager) {
        Manager selmanager = managerDao.selectObjectByUsername(manager.getName());
        if(selmanager == null){
            manager.setPassword(StringUtil.MD5(manager.getPassword()));
            return managerDao.save(manager) > 0;
        }else{
            logger.info("manager已存在");
            return false;
        }

    }

    @Override
    public Manager selectManagerByUsername(String username) {
        Manager manager =  managerDao.selectObjectByUsername(username);
        if("1".equals(manager.getStatus())){
            return manager;
        }else{
            //表明manager已被禁用
            logger.info(username+"已被禁用");
            return null;
        }
    }

    @Override
    public Boolean updateManagerRoomCard(Integer id, Manager manager, Integer payCardNum) {
        Manager selManager = managerDao.selectByPrimaryKey(id);
        if(selManager == null) {
            logger.info("根据id没有找到代理商");
            return false;
        }
        selManager.setActualcard(selManager.getActualcard() + payCardNum);
        selManager.setTotalcards(selManager.getTotalcards() + payCardNum);
        try {
            if(manager.getPowerId() != 1){
            //1：不是超级管理元的情况下，检查mananger的房卡是否够充值
            if(manager.getActualcard() >= Math.abs(payCardNum)){
                //修改两边的房卡
                //TODO 总房卡数没减
                manager.setActualcard(manager.getActualcard() - payCardNum);
                //修改管理员/代理商
                if(managerDao.updateActualcard(manager)>0){
                    int result = managerDao.updateActualcard(selManager);
                    if(result > 0){
                        return true;
                    }
                }


            }else{
                logger.info("房卡数不足");
                return false;
            }
        }
        else{
            //超管操作
               if(managerDao.updateActualcard(selManager)>0){
                   logger.info("更新房卡数量成功");
                   return true;
               }
                  /*  //增加充值记录
                    roomCardRecord(0,managerid, 1, payCardNum);
                    json.put("status", "0");
                    json.put("data", "1");*/
            }
        } catch (Exception e) {
            logger.error("充房卡操作异常"+e.getMessage());
        }
        return false;
    }

    @Override
    public List<Manager> selectObjectsByMap(Map<String, Integer> map) {
        return null;
    }

    @Override
    public JSONObject updateAccountRoomCard(Integer id, Manager manager, Integer payCardNum) {
        return null;
    }



    @Override
    public JSONArray getWinnersInfo(String status) {
        return null;
    }

    @Override
    public JSONArray getPrizesInfo() {
        return null;
    }

    @Override
    public JSONObject updatePrizeInfo(Map<String, Object> map) {
        return null;
    }

    @Override
    public JSONObject saveNewNotice(String notice) {
        return null;
    }

    @Override
    public List<Manager> selectManagerByTel(Map<String, Object> map) {
        return null;
    }

    @Override
    public int updateManagerStatus(Manager manager, Integer managerId) {
        return 0;
    }

    @Override
    public int updateByMap(Map<String, Object> map) {
        return 0;
    }

    @Override
    public JSONObject getAllGameInfos() {
        return null;
    }

    @Override
    public JSONArray roomCardWaterCourse(int managerId) {
        return null;
    }

    @Override
    public JSONArray operationWaterCourse(int managerId) {
        return null;
    }

    @Override
    public int updateManagerInfo(ManagerParam param) {
        return managerDao.updateByParam(param);
    }
}
